package com.jxd.service.impl;

import com.jxd.entity.MesContent;
import com.jxd.entity.MesHead;
import com.jxd.entity.SelectionMess;
import com.jxd.entity.Switchover;
import com.jxd.hbase.utils.QurryTableUntil;
import com.jxd.model.TwoMachineSwitchover;
import com.jxd.repositories.MesHeadRepositories;
import com.jxd.service.TwoMachineSwitchoverMapperService;
import com.jxd.service.TwoMachineSwitchoverService;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IOUtils;
import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;
import org.locationtech.spatial4j.io.ShapeIO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TwoMachineSwitchoverServiceImpl implements TwoMachineSwitchoverService {
    @Autowired
    @Qualifier("mesHeadRepositories")
    MesHeadRepositories mesHeadRepositories;
    @Autowired
    @Qualifier("getHtableByMesHead")
    HTable mesHeadClient;
    @Autowired
    @Qualifier("getHtableByMesContent")
    HTable mesContentClient;
    @Autowired
    private TwoMachineSwitchoverMapperService twoMachineSwitchoverMapperService;

    @Autowired
    private QurryTableUntil qurryTableUntil;


    @Override
    /**
     * 获取当前文件下的心跳报文
     */
    public List<MesHead> getHeartbeatMesBysendDevice(String fileName, String shipId, String mesid) {
        System.out.println(fileName);
        System.out.println(shipId);
        System.out.println(mesid);
        List<MesHead> heartbeatMess = mesHeadRepositories
                .findAllByFileNameAndMesIdAndShipId(fileName, mesid, shipId);
        System.out.println(heartbeatMess.size());
        return heartbeatMess;
    }

    @Override
    public List<Switchover> getMesContentByHeartbeatMess(List<MesHead> heartbeatMess, Connection connection, String tableName) {
        List<Switchover> switchovers = new ArrayList<>();
        List<MesContent> rowkeys_10 = new ArrayList<>();
        List<String> rowkeys_11 = new ArrayList<>();
        List<String> rowkeys_12 = new ArrayList<>();
        for (int i = 0; i < heartbeatMess.size(); i++) {
            MesContent mesContent10 = new MesContent();
            String rowkey10 = heartbeatMess.get(i).getMesSignId() + "-10";
            String mesTime = heartbeatMess.get(i).getMesTime();
            String mesDate = heartbeatMess.get(i).getMesDate();
            mesContent10.setMesDate(mesDate);
            mesContent10.setMesTime(mesTime);
            mesContent10.setRowkey(rowkey10);
            rowkeys_10.add(mesContent10);
            String rowkey11 = heartbeatMess.get(i).getMesSignId() + "-11";
            rowkeys_11.add(rowkey11);
            String rowkey12 = heartbeatMess.get(i).getMesSignId() + "-12";
            rowkeys_12.add(rowkey12);
        }
        List<MesContent> mesContent10 =
                qurryTableUntil.qurryTimeAndVal(rowkeys_10, tableName, connection);
        List<String> vals11 =
                qurryTableUntil.qurryBwContentVal(rowkeys_11, tableName, connection);
        List<String> vals12 =
                qurryTableUntil.qurryBwContentVal(rowkeys_12, tableName, connection);
        for (int i = 0; i < mesContent10.size(); i++) {
            String mesContent = mesContent10.get(i).getCode() + vals11.get(i) + vals12.get(i);
            String mesDate = heartbeatMess.get(i).getMesDate().substring(0, 11);
            String mesTime = heartbeatMess.get(i).getMesTime();
            String mesSignId =
                    String.valueOf(
                            heartbeatMess.get(i).getMesSignId());
            String sendDevice = heartbeatMess.get(i).getSendDevice();
            Switchover switchover = new Switchover();
            switchover.setMesSignId(mesSignId);
            switchover.setMesDate(mesDate);
            switchover.setMesTime(mesTime);
            switchover.setMesContent(mesContent);
            switchover.setSendDevice(sendDevice);
            switchovers.add(switchover);

        }
        return switchovers;
    }

    /**
     * 双机切换统计类
     *
     * @param mesContentByHeartbeatMess
     * @return
     */
    @Override
    public List<TwoMachineSwitchover> getTwoMachineSwitchovers(List<Switchover> mesContentByHeartbeatMess) {
        List<TwoMachineSwitchover> twoMachineSwitchovers = new ArrayList<>();
        for (int i = 0; i < mesContentByHeartbeatMess.size(); i++) {
            //判断第一个是否为101且只有一条
            if (i == 0 && mesContentByHeartbeatMess.size() == 1) {
                if (mesContentByHeartbeatMess.get(i).getMesContent().equals("101")) {
                    System.out.println("双机");
                    TwoMachineSwitchover twoMachineSwitchover = new TwoMachineSwitchover();
                    //切换时间
                    String endTime = mesContentByHeartbeatMess.get(i).getMesDate()
                            + mesContentByHeartbeatMess.get(i).getMesTime();
                    twoMachineSwitchover.setEndtime(endTime);
                    //设备名称
                    twoMachineSwitchover.setSbname(mesContentByHeartbeatMess.get(i).getSendDevice());
                    twoMachineSwitchovers.add(twoMachineSwitchover);
                }
            } else if (i == mesContentByHeartbeatMess.size() - 1
                    && mesContentByHeartbeatMess.size() >= 2) {
                //最后一条 如果与上一条不同且为101 则切换
                if (mesContentByHeartbeatMess.get(i).getMesContent().equals("101") &&
                        !mesContentByHeartbeatMess.get(i).getMesContent().equals(
                                mesContentByHeartbeatMess.get(i - 1).getMesContent())
                ) {
                    System.out.println("双机");
                    TwoMachineSwitchover twoMachineSwitchover = new TwoMachineSwitchover();
                    //切换时间
                    String endTime = mesContentByHeartbeatMess.get(i).getMesDate()
                            + mesContentByHeartbeatMess.get(i).getMesTime();
                    twoMachineSwitchover.setEndtime(endTime);
                    //设备名称
                    twoMachineSwitchover.setSbname(mesContentByHeartbeatMess.get(i).getSendDevice());
                    twoMachineSwitchovers.add(twoMachineSwitchover);
                }
            } else if (mesContentByHeartbeatMess.size() >= 3 &&
                    i < mesContentByHeartbeatMess.size() - 1 &&
                    i > 0) {
                //size>=3 0<i<size-1
                if (mesContentByHeartbeatMess.get(i).getMesContent().equals("101") &&
                        !mesContentByHeartbeatMess.get(i).getMesContent().
                                equals(mesContentByHeartbeatMess.get(i - 1).getMesContent()) &&
                        mesContentByHeartbeatMess.get(i).getMesContent().
                                equals(mesContentByHeartbeatMess.get(i + 1).getMesContent())) {
                    //与前面不同与后面相同且值为101
                    System.out.println("双机");
                    TwoMachineSwitchover twoMachineSwitchover = new TwoMachineSwitchover();
                    //切换时间
                    String endTime = mesContentByHeartbeatMess.get(i).getMesDate()
                            + mesContentByHeartbeatMess.get(i).getMesTime();
                    twoMachineSwitchover.setEndtime(endTime);
                    //设备名称
                    twoMachineSwitchover.setSbname(mesContentByHeartbeatMess.get(i).getSendDevice());
                    twoMachineSwitchovers.add(twoMachineSwitchover);
                }
            }
        }

        return twoMachineSwitchovers;
    }

    @Override
    public String getPowerOnTime(List<MesHead> heartbeatMess) {
        String powerOnTime = "";
        String startTime = Collections.min(heartbeatMess).getMesTime();
        String endTime = Collections.max(heartbeatMess).getMesTime();
        double time = 0.0;
        double hour =
                (Double.parseDouble(endTime.substring(0, 2)) -
                        Double.parseDouble(startTime.substring(0, 2))) * 60;
        double min =
                (Double.parseDouble(endTime.substring(3, 5)) -
                        Double.parseDouble(startTime.substring(3, 5)));
        double second = (
                Double.parseDouble(endTime.substring(6, 8)) -
                        Double.parseDouble(startTime.substring(6, 8))) / 60;
        time = time + hour + min + second;
        powerOnTime = powerOnTime + time;
        return powerOnTime;
    }

    /**
     * 获取当前设备下的工作模式选择报文
     *
     * @param fileName
     * @param shipId
     * @param mesid
     * @param sendDevice
     * @return
     */
    @Override
    public List<MesHead> getSelectionMessBysendDevice(String fileName, String shipId, String mesid, String sendDevice) {
        List<MesHead> selectionMess
                = mesHeadRepositories
                .findAllByFileNameAndShipIdAndSendDeviceAndMesId(
                        fileName,
                        shipId,
                        sendDevice,
                        mesid,
                        Sort.by(Sort.Order.asc("mesDate")));

        return selectionMess;
    }

    /***
     * 获取报文数组
     * @param selectionMess
     * @return
     */
    @Override
    public List<SelectionMess> getSelectionMessContent(List<MesHead> selectionMess,String bwTableName,Connection connection) {
        List<SelectionMess> selectionMessList = new ArrayList<>();
        List<MesContent> rowkeys = new ArrayList<>();
        //封装rowkey数组
        for (int i = 0; i < selectionMess.size(); i++) {
            String mesSignid = selectionMess.get(i).getMesSignId();
            String rowKey_2 = mesSignid + "-2";
            MesContent mesContent  = new MesContent();
            mesContent.setSendDevice(selectionMess.get(i).getSendDevice());
            mesContent.setMesSignId(selectionMess.get(i).getMesSignId());
            mesContent.setMesDate(selectionMess.get(i).getMesDate());
            mesContent.setMesTime(selectionMess.get(i).getMesTime());
        }
        List<MesContent> mesContents=qurryTableUntil.qurryTimeAndVal(rowkeys, bwTableName, connection);
        for (int i = 0; i <mesContents.size() ; i++) {
            SelectionMess workingMes = new SelectionMess();
            workingMes.setSendDevice(mesContents.get(i).getSendDevice());
            workingMes.setMesSignId(mesContents.get(i).getMesSignId());
            workingMes.setMesDate(mesContents.get(i).getMesDate());
            workingMes.setMesTime(mesContents.get(i).getMesTime());
            workingMes.setSelectionMess(mesContents.get(i).getCode());
            selectionMessList.add(workingMes);
        }
        return selectionMessList;
    }

    @Override
    public String getWorkingTime(List<SelectionMess> selectionMessContent) {
        //没有即为0
        String workingTime = "";
        double time = 0;
        for (int i = 0; i < selectionMessContent.size(); i++) {
            double hour = 0;
            double min = 0;
            double s = 0;
            double sts = 0;
            double ends = 0;
            if (selectionMessContent.get(i).getSelectionMess().equals("0001")) {
                hour = Float.parseFloat(selectionMessContent.get(i).getMesTime().substring(0, 2));
                min = Float.parseFloat(selectionMessContent.get(i).getMesTime().substring(3, 5));
                s = Float.parseFloat(selectionMessContent.get(i).getMesTime().substring(6, 8));
                sts = (hour * 60.0 * 60.0 * 1000.0) + (min * 60.0 * 1000) + (s * 1000);
                for (int j2 = i; j2 < selectionMessContent.size(); j2++) {
                    if (!selectionMessContent.get(j2).getSelectionMess().equals("0001")) {
                        i = j2;
                        hour = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(0, 2));
                        min = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(3, 5));
                        s = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(6, 8));
                        ends = (hour * 60.0 * 60.0 * 1000.0) + (min * 60.0 * 1000) + (s * 1000);
                        time = time + (ends - sts);
                        break;

                    } else if (j2 == (selectionMessContent.size() - 1)) {
                        i = j2;
                        hour = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(0, 2));
                        min = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(3, 5));
                        s = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(6, 8));
                        ends = (hour * 60.0 * 60.0 * 1000.0) + (min * 60.0 * 1000) + (s * 1000);
                        time = time + (ends - sts);
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < selectionMessContent.size(); i++) {
            double hour = 0;
            double min = 0;
            double s = 0;
            double sts = 0;
            double ends = 0;
            if (selectionMessContent.get(i).getSelectionMess().equals("0004")) {
                hour = Float.parseFloat(selectionMessContent.get(i).getMesTime().substring(0, 2));
                min = Float.parseFloat(selectionMessContent.get(i).getMesTime().substring(3, 5));
                s = Float.parseFloat(selectionMessContent.get(i).getMesTime().substring(6, 8));
                sts = (hour * 60.0 * 60.0 * 1000.0) + (min * 60.0 * 1000) + (s * 1000);
                for (int j2 = i; j2 < selectionMessContent.size(); j2++) {
                    if (!selectionMessContent.get(j2).getSelectionMess().equals("0004")) {
                        i = j2;
                        hour = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(0, 2));
                        min = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(3, 5));
                        s = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(6, 8));
                        ends = (hour * 60.0 * 60.0 * 1000.0) + (min * 60.0 * 1000) + (s * 1000);
                        time = time + (ends - sts);
                        break;

                    } else if (j2 == (selectionMessContent.size() - 1)) {
                        i = j2;
                        hour = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(0, 2));
                        min = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(3, 5));
                        s = Float.parseFloat(selectionMessContent.get(j2).getMesTime().substring(6, 8));
                        ends = (hour * 60.0 * 60.0 * 1000.0) + (min * 60.0 * 1000) + (s * 1000);
                        time = time + (ends - sts);
                        break;
                    }
                }
            }
        }
        workingTime = workingTime + time;
        return workingTime;
    }

    @Override
    public void insertTwoMachineSwitchover(List<TwoMachineSwitchover> twoMachineSwitchovers,
                                           String workingTime,
                                           String powerOnTime,
                                           String shipid,
                                           String filename,
                                           String xtid) {

        for (int i = 0; i < twoMachineSwitchovers.size(); i++) {
            twoMachineSwitchovers.get(i).setLjgzsj(workingTime);
            twoMachineSwitchovers.get(i).setLjjdsj(powerOnTime);
            twoMachineSwitchovers.get(i).setXtname(shipid);
            twoMachineSwitchovers.get(i).setFilename(filename);
            twoMachineSwitchovers.get(i).setXtid(xtid);
            twoMachineSwitchoverMapperService
                    .insertTwoMachineSwitchover(twoMachineSwitchovers.get(i));
        }
    }
}
